import { Model } from '../../model';

/**
 * 当前次提示计算的模型上下文
 *
 * @author chitanda
 * @date 2022-01-13 14:01:45
 * @export
 * @class ContextModel
 */
export class ContextModel {
  protected model: Model = Model.getInstance();

  /**
   * 按照域次序排序的模型上下文
   *
   * @author chitanda
   * @date 2022-01-13 14:01:31
   * @type {IModel[]}
   */
  readonly models: IModel[] = [];

  add(model: IModel): void {
    this.models.push(model);
  }

  getModel(key: string): IModel | string | null {
    for (let i = this.models.length - 1; i >= 0; i--) {
      const model = this.models[i];
      const val = model[key];
      if (val) {
        return val;
      }
    }
    return null;
  }

  /**
   * 获取最后的模型
   *
   * @author chitanda
   * @date 2022-01-13 15:01:22
   * @return {*}  {IModel}
   */
  getLastModel(): IModel {
    if (this.models.length > 0) {
      return this.models[this.models.length - 1];
    }
    return {};
  }

  /**
   * 获取全局模型
   *
   * @author chitanda
   * @date 2022-01-13 15:01:40
   * @return {*}  {IModel}
   */
  getGlobalModel(): IModel {
    if (this.models.length > 0) {
      return this.models[0];
    }
    return {};
  }
}
